﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EraNet.Core;
using EraNet.Core.Model.SysManage;
using EraNet.Core.Data;
using EraNet.Core.Caching;
using EraNet.Common;
namespace EraNet.Services.SysManage
{
    public  class OSAInterfaceServices:IOSAInterfaceServices
    {
        #region Constants
        private const string NOTICES_BY_ID_KEY = "OSAInterface.Id-{0}";
        #endregion
        #region Ctor
        private readonly IRepository<OSAInterface> _osainterfaceRepository;
        private readonly IRepository<OSAInterfaceApply> _osainterfaceapplyRepository;
        private readonly ICacheManager _cacheManager;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="adminRepository"></param>
        /// <param name="cacheManager"></param>
        public OSAInterfaceServices(IRepository<OSAInterfaceApply> osainterfaceapplyRepository, IRepository<OSAInterface> osainterfaceRepository, ICacheManager cacheManager)
        {
            _osainterfaceRepository = osainterfaceRepository;
            _osainterfaceapplyRepository = osainterfaceapplyRepository;
            _cacheManager = cacheManager;
        }
        #endregion
        #region Methods
        /// <summary>
        /// 插入新数据
        /// author:david
        /// time  :2013-6-9
        /// </summary>
        /// <param name="model"></param>
        public virtual int Insert(OSAInterface model)
        {
            if (model == null) throw new ExtException("Null Model");
            if (!Validator.IsURL(model.InterfaceUrl)) throw new ExtException("Error Url Format");
            return  _osainterfaceRepository.Insert(model);
        }
        /// <summary>
        /// 更新数据
        /// author:david
        /// time:  2013-6-9
        /// </summary>
        /// <param name="model"></param>
        public virtual int  Update(OSAInterface model)
        {
            if (model == null) throw new ExtException("Null Model");
            if (!Validator.IsURL(model.InterfaceUrl)) throw new ExtException("Error Url Format");
            return _osainterfaceRepository.Update(model);
        }
        /// <summary>
        /// 将数据标识成删除状态
        /// author:david
        /// time  :2013-6-9
        /// </summary>
        /// <param name="modelId"></param>
        public virtual int Delete(int modelId)
        {
            if (modelId <= 0) throw new ExtException("Null Model");
            var model = _osainterfaceRepository.GetById(modelId);
            model.Deleted = true;
            return  _osainterfaceRepository.Update(model);
        }
        /// <summary>
        /// 根据ID获取实体
        /// author:david
        /// time  :2013-6-9
        /// </summary>
        /// <param name="noticeId"></param>
        /// <returns></returns>
        public OSAInterface GetOsaById(int modelId)
        {
            if (modelId <= 0) return null;
            return _osainterfaceRepository.GetById(modelId);
        }
        /// <summary>
        /// 分页获取所有未删除的数据
        /// author:david
        /// time  :2013-6-9
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IList<OSAInterface> SelectPager(int PageIndex, int PageSize, out int PageCount)
        {
            var query = _osainterfaceRepository.Table;
            query = query.Where(a => !a.Deleted);
            query = query.OrderBy(a => a.Id);
            PageCount = query.Count();
            var list = new PagedList<OSAInterface>(query, PageIndex, PageSize);
            return list;
        }
        /// <summary>
        /// 根据类型，状态，分页获取数据
        /// author:david
        /// time  :2013-6-9
        /// </summary>
        /// <param name="InterfaceType"></param>
        /// <param name="Status"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IList<OSAInterface> SelectPager(int InterfaceType, int Status, int PageIndex, int PageSize, out int PageCount)
        {
            var query = _osainterfaceRepository.Table;
            query = query.Where(a => !a.Deleted);
            if (InterfaceType > 0) query = query.Where(c => c.InterfaceType == InterfaceType);
            if(Status>=0) query = query.Where(a => a.InterfaceStatus == Status);
            query = query.OrderBy(a => a.Id);
            PageCount = query.Count();
            var list = new PagedList<OSAInterface>(query, PageIndex, PageSize);
            return list;
        }
        #endregion

        #region Methods
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int InsertApply(OSAInterfaceApply model)
        {
            if (model == null) throw new ExtException("Null Model");
            if (!Validator.IsPhone(model.Telephone)&&!Validator.IsMobile(model.Telephone)) throw new ExtException("Error Telephone Format");
            return _osainterfaceapplyRepository.Insert(model);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateApply(OSAInterfaceApply model)
        {
            if (model == null) throw new ExtException("Null Model");
            if (!Validator.IsPhone(model.Telephone) && !Validator.IsMobile(model.Telephone)) throw new ExtException("Error Telephone Format");
            return _osainterfaceapplyRepository.Update(model);
        }
        /// <summary>
        /// 删除指定的对象
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteApply(int modelId)
        {
            if (modelId <= 0) throw new ExtException("Null Model");
            var model = _osainterfaceapplyRepository.GetById(modelId);
            model.Deleted = true;
            return _osainterfaceapplyRepository.Update(model);
        }
        /// <summary>
        /// 根据ID获取信息
        /// </summary>
        /// <param name="noticeId"></param>
        /// <returns></returns>
        public OSAInterfaceApply GetApplyById(int modelId)
        {
            if (modelId <= 0) return null;
            return _osainterfaceapplyRepository.GetById(modelId);
        }
        /// <summary>
        /// 批量获取数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IList<OSAInterfaceApply> ApplySelectPager(int PageIndex, int PageSize, out int PageCount)
        {
            var query = _osainterfaceapplyRepository.Table;
            query = query.Where(a => !a.Deleted);
            query = query.OrderBy(a => a.Id);
            PageCount = query.Count();
            var list = new PagedList<OSAInterfaceApply>(query, PageIndex, PageSize);
            return list;
        }
        /// <summary>
        /// 获取当前指定ID下的公告列表
        /// </summary>
        /// <param name="SchoolId"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Pagesize"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IList<OSAInterfaceApply> ApplySelectPager(int InterfaceType, int Status, int PageIndex, int PageSize, out int PageCount)
        {
            var query = _osainterfaceapplyRepository.Table;
            query = query.Where(a => a.Status == Status);
            query = query.Where(a => !a.Deleted);
            query = query.OrderBy(a => a.Id);
            PageCount = query.Count();
            var list = new PagedList<OSAInterfaceApply>(query, PageIndex, PageSize);
            return list;
        }
        #endregion

        #region 
        public IList<OSAEntityList> SelectPagers(int PageIndex, int PageSize,out int PageCount)
        {
            PageCount = 0;

            int countindex=(from c in _osainterfaceapplyRepository.Table select c.Id).Count();

            var query = from p in _osainterfaceRepository.Table
                        //let o = from c in _osainterfaceapplyRepository.Table where  c.InterfaceId==p.Id select c.Id
                        select new OSAEntityList
                              { CreateDate=p.CreateDate, 
                                DevelopmentLanguage=p.DevelopmentLanguage,
                                ExpirationDate=p.ExpirationDate,
                                InterfaceName=p.InterfaceName,
                                ApplyCount= 1
                              };
            PageCount = query.Count();
            query = query.OrderBy(c => c.CreateDate);
            query = query.Skip((PageIndex - 1) * PageSize).Take(PageSize);
            return query.ToList();
        }
        #endregion
    }
}
